/*******************************************************************
 *
 * Copyright (C), 2021-2022, LS, All rights reserved.
 *
 * Author      : 
 * Create Time : 2021-09-22
 * Version     : v0.1
 * Description : 
 *******************************************************************/
#ifndef _APP_TIME_H_
#define _APP_TIME_H_
 
/*----------------------------Head file----------------------------*/
#include "types.h"

/*----------------------------macro file---------------------------*/
#define DATETIME_SYNC_INTERVAL      (60*1000) //interval is 60s

#define DTM2TM(tm, dtm)		{	(tm)->tm_mon = (dtm)->month-1;\
                                (tm)->tm_year = (dtm)->year-1900;\
                                (tm)->tm_mday = (dtm)->day;\
                                (tm)->tm_hour = (dtm)->hour;\
                                (tm)->tm_min = (dtm)->minutes;\
                                (tm)->tm_sec = (dtm)->seconds;}


#define TM2DTM(dtm, tm)		{	(dtm)->month = (tm)->tm_mon+1;\
                                (dtm)->year = (tm)->tm_year+1900;\
                                (dtm)->day = (tm)->tm_mday;\
                                (dtm)->hour = (tm)->tm_hour;\
                                (dtm)->minutes = (tm)->tm_min;\
                                (dtm)->seconds = (tm)->tm_sec;}


#define BCDTM2DTM(dtm, bcdtm)	{	(dtm)->year = bcdtm[0] + 2000;\
                                    (dtm)->month = bcdtm[1];\
                                    (dtm)->day = bcdtm[2];\
                                    (dtm)->hour = bcdtm[3];\
                                    (dtm)->minutes = bcdtm[4];\
                                    (dtm)->seconds = bcdtm[5];}

#define DTM2BCDTM(bcdtm, dtm)	{	bcdtm[0] = (dtm)->year - 2000;\
                                    bcdtm[1] = (dtm)->month;\
                                    bcdtm[2] = (dtm)->day;\
                                    bcdtm[3] = (dtm)->hour;\
                                    bcdtm[4] = (dtm)->minutes;\
                                    bcdtm[5] = (dtm)->seconds;}

#define DTM2U32(u32val, dtm)	{	uint32 tmp = (dtm)->year;	u32val = (tmp << 16);\
                                    tmp = (dtm)->month; u32val |= (tmp << 8);\
                                    tmp = (dtm)->day; u32val |= tmp;}

#define U322DTM(dtm,u32val)	{	(dtm)->year = (uint16)((u32val)>>16);\
                                    (dtm)->month = (uint8)(((u32val) >> 8) & 0xff);\
                                    (dtm)->day = (uint8)((u32val) &0xff);\
                                    (dtm)->hour = 0; (dtm)->minutes  =0; (dtm)->seconds = 0;}

/*----------------------------type define--------------------------*/
#pragma pack(1)

typedef struct
{
    uint64_t tm_base;
    uint32_t snapshot;
} datetime_cfg_t;


typedef struct{
    uint16_t id;
    datetime_cfg_t info;
} timebase_t;

#pragma pack()
									
typedef struct
{
    uint8_t seconds;
    uint8_t minutes;
    uint8_t hour;
    uint8_t day;
    uint8_t month;
    uint16_t year;
} app_time_rtc_t;

/*----------------------------var define---------------------------*/

/*-------------------------func declaration------------------------*/
extern void app_time_set_dt_cfg(datetime_cfg_t *cfg);
extern uint64_t app_time_tim_base_get(void);
extern uint32_t app_time_snapshot_get(void);

extern void app_datetime_init(void);
extern void app_datetime_sync_handler(void);
extern void app_time_uni_utc_set(uint32_t uni_utc);
extern uint32_t app_time_universal_utc_get(void);
extern uint64_t app_time_base_get(void);
extern void app_set_default_datetime(void);

/*-----------------------------------------------------------------*/


#endif


